/*
 ** Author: hait
 ** Time: 2015.4.7
 *  对所有data-submit属性绑定事件
 */
 
 //处理submit事件 自带验证Buff
 $(document).on("click.form.submit","[data-submit]",function(){
 	var $elem = $(this),
 		elems = $elem.closest("form")[0].elements,
 		err = $elem.data("err"),
 		data_push = {
 			url : $.Depath.getURL($elem.data("submit")),
 			data : {}
 		};

 	//console.log(elems);
 	var arr=new Array();
 	for(var i=0,len=elems.length;i<len;i++){
 		if($.verify_test($(elems[i]).data("verify"),elems[i].value) == false){
 			$.toast("error","提交信息不完整或格式错误");
 			return;	
 		}
 		if(elems[i].name=="access")
 		{
 			if(elems[i].checked)
 				arr.push(elems[i].value);
 		}else if(elems[i].name=="role"){
 			if(elems[i].checked)
 				data_push["data"][elems[i].name] = elems[i].value;
 		}else
 			data_push["data"][elems[i].name] = elems[i].value;
 	}
 	data_push["data"]["access"]=arr;
 	 if(editor!=undefined)
	 {
		 console.log(editor.html());
		 data_push["data"]["context"]=editor.html();//阿冰弄得公告录入的玩意儿
	 }
	 // console.log("First...");
 	 //console.log(data_push);
 	$.Ajax.postData(data_push,formCallback);
 });
 
//form表单提交回调函数
function formCallback(data){
	//console.log(data);
	
	if (data.status == 0) {
		$.toast("success", data.message);
		if (data.data.redirectUrl) {
			window.location.hash = data.data.redirectUrl;
			//console.log("Start...");
			//console.log(data.data.redirectUrl);
		}
		else{
			$(document).trigger("Depath");
		}
	}
	else{
		$.toast("error","操作失败,失败原因："+data.message);
	}
}

//treeview 中a标签的nopath事件
 $(document).on('click','[data-treeview] a', function  (event) {
 	var $elem = $(this);
 	var uri = $elem.data("nopath");
 	 //判断是否有参数
    var args = uri.split("?");
    var levelTemp = args[1].split("=");
    var level = levelTemp[1];
	data_push = {
		url : $.Depath.getURL($elem.data("nopath")),
		data : {}
	};
	data_push["data"]["level"] = level;

 	$.Ajax.postData(data_push,formCallback);
 });
